package com.car.common.model;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;



import com.car.common.model.base.BaseCard;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.plugin.ehcache.IDataLoader;
import com.utils.AlphaStr;
import com.utils.PicUtil;
import com.utils.StringUtil;
import com.utils.SystemClock;
import com.utils.Toolkit;

/**
 * Generated by chengjun.
 */
@SuppressWarnings("serial")
public class Card extends BaseCard<Card> {
	
	public final static Card dao = new Card();

	public ArrayList<Map<String,Object>> sel_time(Long user_id,int page) {
		// TODO 私密空间 
		String andSql = " where a.alt_tj = 2  and a.is_show = 1 and a.create_time < "+ SystemClock.now()/1000L +" order by  a.create_time desc limit " + (page -1) * 10 +" , 10 " ;
		List<Card> cardList = dao.find(" select a.card_type,a.ids,a.id,a.user_id,a.content, FROM_UNIXTIME(a.create_time,'%m-%d') as create_time_string,a.href_val,a.images,a.views,a.discuss as discuss,a.heat,a.fabulous,a.tags_id,a.topic_id from card as a " + andSql);
		ArrayList<Map<String,Object>> result = Lists.newArrayList();
		for(Card one: cardList) {
			Db.update("update card set t_views = t_views + 1 , views = views + 1 where id = " + one.get("id"));
			Map<String,Object> temp = Maps.newHashMap();
			temp.put("card_type",     one.get("card_type"));
			temp.put("id",            one.get("id"));
			temp.put("user_id",       one.get("user_id"));
			temp.put("content",       one.get("content"));
			temp.put("create_time",   one.get("create_time_string"));
			temp.put("href_val",      one.get("href_val"));
			temp.put("views",         one.get("views"));
			temp.put("discuss",       one.get("discuss"));
			temp.put("heat",          one.get("heat"));
			temp.put("fabulous",      one.get("fabulous"));
			temp.put("tags_id",       one.get("tags_id"));
			List<CardImg> images = CardImg.dao.find("select card_id,image_url from card_img where card_id = " + one.getId());
			List<String> imges = Lists.newLinkedList();
			List<String> imagesStr = Lists.newArrayList();
			for(CardImg img :images) {
				imagesStr.add(PicUtil.QINIU_DIR + img.getImageUrl());
				//img.setImageUrl((PicUtil.QINIU_DIR + img.getImageUrl()));
			}
			temp.put("images",  imges);
			// @ 逻辑
			if (StringUtil.empty(one.getStr("ids"))) {
				temp.put("ids", Lists.newArrayList());
			} else {
				List<Users> users = Users.dao.find("select id,nick_name from users where id in (" + one.getStr("ids") +")");
				for(Users u : users) {
					u.setNickName(StringUtil.decode(u.getNickName()));
				}
				temp.put("ids", users);
			}
			//是否点赞
			if (user_id == 0L) {
				temp.put("is_like", 0);
			} else {
				CardLike like = CardLike.dao.findFirst("select * from card_like where card_id = " + one.getId() + " and status = 1 and " + " user_id =" + user_id);
				temp.put("is_like", like == null ? 0 :1 );
			}
			// 标签 topics
			if (StringUtil.empty(one.getStr("topic_id"))) {
				temp.put("topics", Lists.newArrayList());
			} else {
				List<Topics> topics = Topics.dao.find("select name from topics where id in (" + one.getStr("topic_id") +")");
				temp.put("topics", topics);
			}
			if (user_id == 0L) {
				temp.put("is_collect", 0);
			} else {
				CardCollect collect = CardCollect.dao.findFirst("select * from card_collect where card_id = " + one.getId() + " and status = 1 and " + " user_id =" + user_id);
				temp.put("is_collect", collect == null ? 0 :1 );
			}
			//temp.put("share_url", PicUtil.DOMAIN +"shares/index?card_id=" + one.getId());
			temp.put("is_all", AlphaStr.is_all(StringUtil.decode(one.getContent()), 120, true));
			
			String sql = "select u.*,ae.name as area_name, FROM_UNIXTIME(u.birthday,'%Y年%m月%d日') as birthday_string,u.is_true, CASE u.sex  WHEN '1' THEN '男' WHEN '2' THEN '女' WHEN '3' THEN 'CD' WHEN '4' THEN 'FtM' WHEN '5' THEN 'MtF'  ELSE '其他' END as sex_name ";
			sql += ", FROM_UNIXTIME(u.create_time,'%Y-%m-%d %H:%i') as create_time_string , FROM_UNIXTIME(u.vip_end_time,'%Y-%m-%d') as vip_end_time_string,u.auth_badge,u.talk_badge,u.vip_badpe,u.consume_badpe,u.credit from users as u left join area ae on ae.code = u.area_id where u.id = " + one.getUserId();
			
			Users user = Users.dao.findFirst(sql);
			if (user == null) {
				temp.put("user_data", Lists.newArrayList());
			} else {
				temp.put("user_data", Users.dao.info(user,null));
			}
			result.add(temp);
			
		}
		return result;
	}
	
	
	public ArrayList<Map<String,Object>> my(Long user_id,int page) {
		String andSql = " where (discuss.user_id = "+user_id +" or a.user_id = " + user_id + ") and a.is_show = 1 and a.create_time < "+ SystemClock.now()/1000L +" order by  a.create_time desc limit " + (page -1) * 10 +" , 10 " ;
		List<Card> cardList = dao.find(" select a.card_type,a.ids,a.id,a.user_id,a.content, FROM_UNIXTIME(a.create_time,'%m-%d') as create_time_string,a.href_val,a.images,a.views,a.discuss as discuss,a.heat,a.fabulous,a.tags_id,a.topic_id from card as a  LEFT JOIN card_discuss as discuss on discuss.card_id = a.id  " + andSql);
		ArrayList<Map<String,Object>> result = Lists.newArrayList();
		for(Card one: cardList) {
			Db.update("update card set t_views = t_views + 1 , views = views + 1 where id = " + one.get("id"));
			Map<String,Object> temp = Maps.newHashMap();
			temp.put("card_type",     one.get("card_type"));
			temp.put("id",            one.get("id"));
			temp.put("user_id",       one.get("user_id"));
			temp.put("content",        StringUtil.decode(one.getStr("content")));
			temp.put("create_time",   one.get("create_time_string"));
			temp.put("href_val",      one.get("href_val"));
			temp.put("views",         one.get("views"));
			temp.put("discuss",       one.get("discuss"));
			temp.put("heat",          one.get("heat"));
			temp.put("fabulous",      one.get("fabulous"));
			temp.put("tags_id",       one.get("tags_id"));
			//temp.put("images",        one.get("images"));  图片
			List<CardImg> images = CardImg.dao.find("select card_id,image_url from card_img where card_id = " + one.getId());
			List<String> imagesStr = Lists.newArrayList();
			for(CardImg img :images) {
				imagesStr.add(PicUtil.QINIU_DIR + img.getImageUrl());
				//img.setImageUrl((PicUtil.QINIU_DIR + img.getImageUrl()));
			}
			temp.put("images",  imagesStr);
			// @ 逻辑
			if (StringUtil.empty(one.getStr("ids"))) {
				temp.put("ids", Lists.newArrayList());
			} else {
				List<Users> users = Users.dao.find("select id,nick_name from users where id in (" + one.getStr("ids") +")");
				for(Users u : users) {
					u.setNickName(StringUtil.decode(u.getNickName()));
				}
				temp.put("ids", users);
			}
			//是否点赞
			if (user_id == 0L) {
				temp.put("is_like", 0);
			} else {
				CardLike like = CardLike.dao.findFirst("select * from card_like where card_id = " + one.getId() + " and status = 1 and " + " user_id =" + user_id);
				temp.put("is_like", like == null ? 0 :1 );
			}
			// 标签 topics
			if (StringUtil.empty(one.getStr("topic_id"))) {
				temp.put("topics", Lists.newArrayList());
			} else {
				List<Topics> topics = Topics.dao.find("select name from topics where id in (" + one.getStr("topic_id") +")");
				temp.put("topics", topics);
			}
			if (user_id == 0L) {
				temp.put("is_collect", 0);
			} else {
				CardCollect collect = CardCollect.dao.findFirst("select * from card_collect where card_id = " + one.getId() + " and status = 1 and " + " user_id =" + user_id);
				temp.put("is_collect", collect == null ? 0 :1 );
			}
			//temp.put("share_url", PicUtil.DOMAIN +"shares/index?card_id=" + one.getId());
//			temp.put("is_all", AlphaStr.is_all(StringUtil.decode(one.getContent()), 120, true));
			
			String sql = "select u.*,ae.name as area_name, FROM_UNIXTIME(u.birthday,'%Y年%m月%d日') as birthday_string,u.is_true, CASE u.sex  WHEN '1' THEN '男' WHEN '2' THEN '女' WHEN '3' THEN 'CD' WHEN '4' THEN 'FtM' WHEN '5' THEN 'MtF'  ELSE '其他' END as sex_name ";
			sql += ", FROM_UNIXTIME(u.create_time,'%Y-%m-%d %H:%i') as create_time_string , FROM_UNIXTIME(u.vip_end_time,'%Y-%m-%d') as vip_end_time_string,u.auth_badge,u.talk_badge,u.vip_badpe,u.consume_badpe,u.credit from users as u left join area ae on ae.code = u.area_id where u.id = " + one.getUserId();
			
			Users user = Users.dao.findFirst(sql);
			if (user == null) {
				temp.put("user_data", Lists.newArrayList());
			} else {
				temp.put("user_data", Users.dao.info(user,null));
			}
			result.add(temp);
			
		}
		return result;
	}
	
	public ArrayList<Map<String,Object>> my_collect(Long user_id,int page) {
		// TODO 私密空间 
		String andSql = " where c.user_id = " + user_id + " and c.status = 1  and a.is_show = 1 and a.create_time < "+ SystemClock.now()/1000L +" order by  a.create_time desc limit " + (page -1) * 10 +" , 10 " ;
		List<Card> cardList = dao.find(" select a.card_type,a.ids,a.id,a.user_id,a.content, FROM_UNIXTIME(a.create_time,'%m-%d') as create_time_string,a.href_val,a.images,a.views,a.discuss as discuss,a.heat,a.fabulous,a.tags_id,a.topic_id from card as a left join card_collect as c on c.card_id = a.id " + andSql);
		ArrayList<Map<String,Object>> result = Lists.newArrayList();
		for(Card one: cardList) {
			Db.update("update card set t_views = t_views + 1 , views = views + 1 where id = " + one.get("id"));
			Map<String,Object> temp = Maps.newHashMap();
			temp.put("card_type",     one.get("card_type"));
			temp.put("id",            one.get("id"));
			temp.put("user_id",       one.get("user_id"));
			temp.put("content",        StringUtil.decode(one.getStr("content")));
			temp.put("create_time",   one.get("create_time_string"));
			temp.put("href_val",      one.get("href_val"));
			temp.put("views",         one.get("views"));
			temp.put("discuss",       one.get("discuss"));
			temp.put("heat",          one.get("heat"));
			temp.put("fabulous",      one.get("fabulous"));
			temp.put("tags_id",       one.get("tags_id"));
			//temp.put("images",        one.get("images"));  图片
			List<CardImg> images = CardImg.dao.find("select card_id,image_url from card_img where card_id = " + one.getId());
			List<String> imagesStr = Lists.newArrayList();
			for(CardImg img :images) {
				imagesStr.add(PicUtil.QINIU_DIR + img.getImageUrl());
				//img.setImageUrl((PicUtil.QINIU_DIR + img.getImageUrl()));
			}
			temp.put("images",  imagesStr);
			// @ 逻辑
			if (StringUtil.empty(one.getStr("ids"))) {
				temp.put("ids", Lists.newArrayList());
			} else {
				List<Users> users = Users.dao.find("select id,nick_name from users where id in (" + one.getStr("ids") +")");
				for(Users u : users) {
					u.setNickName(StringUtil.decode(u.getNickName()));
				}
				temp.put("ids", users);
			}
			//是否点赞
			if (user_id == 0L) {
				temp.put("is_like", 0);
			} else {
				CardLike like = CardLike.dao.findFirst("select * from card_like where card_id = " + one.getId() + " and status = 1 and " + " user_id =" + user_id);
				temp.put("is_like", like == null ? 0 :1 );
			}
			// 标签 topics
			if (StringUtil.empty(one.getStr("topic_id"))) {
				temp.put("topics", Lists.newArrayList());
			} else {
				List<Topics> topics = Topics.dao.find("select name from topics where id in (" + one.getStr("topic_id") +")");
				temp.put("topics", topics);
			}
			if (user_id == 0L) {
				temp.put("is_collect", 0);
			} else {
				CardCollect collect = CardCollect.dao.findFirst("select * from card_collect where card_id = " + one.getId() + " and status = 1 and " + " user_id =" + user_id);
				temp.put("is_collect", collect == null ? 0 :1 );
			}
			
			String sql = "select u.*,ae.name as area_name, FROM_UNIXTIME(u.birthday,'%Y年%m月%d日') as birthday_string,u.is_true, CASE u.sex  WHEN '1' THEN '男' WHEN '2' THEN '女' WHEN '3' THEN 'CD' WHEN '4' THEN 'FtM' WHEN '5' THEN 'MtF'  ELSE '其他' END as sex_name ";
			sql += ", FROM_UNIXTIME(u.create_time,'%Y-%m-%d %H:%i') as create_time_string , FROM_UNIXTIME(u.vip_end_time,'%Y-%m-%d') as vip_end_time_string,u.auth_badge,u.talk_badge,u.vip_badpe,u.consume_badpe,u.credit from users as u left join area ae on ae.code = u.area_id where u.id = " + one.getUserId();
			
			Users user = Users.dao.findFirst(sql);
			if (user == null) {
				temp.put("user_data", Lists.newArrayList());
			} else {
				temp.put("user_data", Users.dao.info(user,null));
			}
			result.add(temp);
			
		}
		return result;
	}
	
	public ArrayList<Map<String,Object>> week_newest(Long user_id){
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_MONTH , -8);
		String andSqlTime = "";
		if (user_id > 1L) {
			List<UserBlack> black_users = UserBlack.dao.find("select id,status,user_id,black_user_id from user_black where user_id = " + user_id);
			for(UserBlack one :black_users) {
				andSqlTime += " and a.user_id != "+ one.getBlackUserId();
			}
		}
		String andSql = " where (a.zan_cnt + a.discuss + a.discuss_cnt + a.heat) > 0 " + andSqlTime + " and a.create_time < "+ SystemClock.now()/1000L + " and a.create_time > " + cal.getTimeInMillis()/1000L +" and a.is_show = 1 order by (a.zan_cnt + a.discuss_cnt) desc , a.create_time desc limit 50 " ;
		List<Card> cardList = dao.find("Select a.card_type,a.ids,a.id,a.user_id,a.content, FROM_UNIXTIME(a.create_time,'%m-%d') as create_time_string,a.href_val,a.images,a.views,a.discuss as discuss,a.discuss,a.heat,a.fabulous,a.tags_id,a.topic_id,u.user_header,u.sex,u.birthday,u.tag_ids,u.nick_name,u.area_id,CASE u.sex  WHEN '1' THEN '男' WHEN '2' THEN '女' WHEN '3' THEN 'CD' WHEN '4' THEN 'FtM' WHEN '5' THEN 'MtF'  ELSE '其他' END as sex_name  from card as a left join users u on u.id = a.user_id " + andSql);
		ArrayList<Map<String,Object>> result = Lists.newArrayList();
		for(Card one: cardList) {
			Db.update("update card set t_views = t_views + 1 , views = views + 1 where id = " + one.get("id"));
			Map<String,Object> temp = Maps.newHashMap();
			temp.put("card_type",     one.get("card_type"));
			temp.put("id",            one.get("id"));
			temp.put("user_id",       one.get("user_id"));
			temp.put("content",       StringUtil.decode(one.getContent()));
			temp.put("create_time",   one.get("create_time_string"));
			temp.put("href_val",      one.get("href_val"));
			temp.put("views",         one.get("views"));
			temp.put("discuss",       one.get("discuss"));
			temp.put("heat",          one.get("heat"));
			temp.put("fabulous",      one.get("fabulous"));
			temp.put("tags_id",       one.get("tags_id"));
			
			List<CardImg> images = CardImg.dao.find("select card_id,image_url from card_img where card_id = " + one.getId());
			List<String> imagesStr = Lists.newArrayList();
			for(CardImg img :images) {
				imagesStr.add(PicUtil.QINIU_DIR + img.getImageUrl());
				//img.setImageUrl((PicUtil.QINIU_DIR + img.getImageUrl()));
			}
			temp.put("images",  imagesStr);
//			// @ 逻辑
			if (StringUtil.empty(one.getStr("ids"))) {
				temp.put("ids", Lists.newArrayList());
			} else {
				List<Users> users = Users.dao.find("select id,nick_name from users where id in (" + one.getStr("ids") +")");
				for(Users u : users) {
					u.setNickName(StringUtil.decode(u.getNickName()));
				}
				temp.put("ids", users);
			}
			//是否点赞
			if (user_id == 0L) {
				temp.put("is_like", 0);
			} else {
				CardLike like = CardLike.dao.findFirst("select * from card_like where card_id = " + one.getId() + " and status = 1 and " + " user_id =" + user_id);
				temp.put("is_like", like == null ? 0 :1 );
			}
			// 标签 topics
			if (StringUtil.empty(one.getStr("topic_id"))) {
				temp.put("topics", Lists.newArrayList());
			} else {
				List<Topics> topics = Topics.dao.find("select name from topics where id in (" + one.getStr("topic_id") +")");
				temp.put("topics", topics);
			}
			if (user_id == 0L) {
				temp.put("is_collect", 0);
			} else {
				CardCollect collect = CardCollect.dao.findFirst("select * from card_collect where card_id = " + one.getId() + " and status = 1 and " + " user_id =" + user_id);
				temp.put("is_collect", collect == null ? 0 :1 );
			}
			String sql = "select u.*,ae.name as area_name, FROM_UNIXTIME(u.birthday,'%Y年%m月%d日') as birthday_string,u.is_true, CASE u.sex  WHEN '1' THEN '男' WHEN '2' THEN '女' WHEN '3' THEN 'CD' WHEN '4' THEN 'FtM' WHEN '5' THEN 'MtF'  ELSE '其他' END as sex_name ";
			sql += ", FROM_UNIXTIME(u.create_time,'%Y-%m-%d %H:%i') as create_time_string , FROM_UNIXTIME(u.vip_end_time,'%Y-%m-%d') as vip_end_time_string,u.auth_badge,u.talk_badge,u.vip_badpe,u.consume_badpe,u.credit from users as u left join area ae on ae.code = u.area_id where u.id = " + one.getUserId();
			
			Users user = Users.dao.findFirst(sql);
			if (user == null) {
				temp.put("user_data", Lists.newArrayList());
			} else {
				temp.put("user_data", Users.dao.info(user,"card_list"));
			}
			result.add(temp);
			
		}
		return result;
	}

	public ArrayList<Map<String, Object>> sel_topic(Long user_id, int page, int type, int last_id) {
		String andSqlTime = null;
		if(last_id > 0) {
			Card card = Card.dao.findById(last_id);
			if (card != null) {
				andSqlTime = " and a.create_time < "+ card.getCreateTime();
			} else {
				andSqlTime = " and a.create_time < "+ SystemClock.now()/1000L;
			}
		} else {
			andSqlTime = " and a.create_time < "+ SystemClock.now()/1000L;
		}
		if (user_id > 1L) {
			List<UserBlack> black_users = UserBlack.dao.find("select id,status,user_id,black_user_id from user_black where user_id = " + user_id);
			for(UserBlack one :black_users) {
				andSqlTime += " and a.user_id != "+ one.getBlackUserId();
			}
		}
		List<Card> cardList = null;
		ArrayList<Map<String,Object>> result = Lists.newArrayList();
		String is_top = "";
		if(type == 1) {
			is_top = " a.is_top desc, ";
		}
		String andSql = " where  a.is_show = 1 " + andSqlTime+ " order by " + is_top + " a.create_time desc limit " + (page -1) * 15 +" , 15 " ;
		cardList = Card.dao.find(" select a.id,a.img_check,a.update_time,a.alt_tj,a.is_show,a.is_check,a.is_private,a.sort_heat,a.card_type,a.ids,a.id,a.user_id,a.content, FROM_UNIXTIME(a.create_time,'%m-%d') as create_time_string,a.href_val,a.images,a.views,a.t_views,a.discuss as discuss,a.t_discuss,a.discuss_cnt,a.heat,a.t_heat,a.fabulous,a.t_fabulous,a.tags_id,a.topic_id,a.sort_heat,a.tsort_heat from card as a " + andSql);
		for(Card one: cardList) {
			Db.update("update card set t_views = t_views + 1 , views = views + 1 where id = " + one.get("id"));
			Map<String,Object> temp = Maps.newHashMap();
			temp.put("card_type",     one.get("card_type"));
			temp.put("id",            one.get("id"));
			temp.put("user_id",       one.get("user_id"));
			temp.put("content",       StringUtil.decode(one.getStr("content")) );
			temp.put("create_time",   one.get("create_time_string"));
			temp.put("href_val",      one.get("heat"));
			temp.put("views",         one.get("views"));
			temp.put("t_views",       one.get("t_views"));
			temp.put("discuss",       one.get("discuss"));
			temp.put("t_discuss",     one.get("t_discuss"));
			temp.put("heat",          one.get("heat"));
			temp.put("t_heat",        one.get("t_heat"));
			temp.put("fabulous",      one.get("fabulous"));
			temp.put("t_fabulous",    one.get("t_fabulous"));
			temp.put("tags_id",       one.get("tags_id"));
			temp.put("sort_heat",     one.get("sort_heat"));
			temp.put("tsort_heat",    one.get("tsort_heat"));
//			temp.put("img_check",     one.get("img_check"));
			temp.put("update_time",   one.get("update_time"));
			/*
			temp.put("alt_tj",        one.get("alt_tj"));
			temp.put("is_show",       one.get("is_show"));
			temp.put("is_top",        one.get("is_top"));
			temp.put("is_check",      one.get("is_check"));
			temp.put("is_private",    one.get("is_private"));
			*/
			//temp.put("images",        one.get("images"));  图片
			List<CardImg> images = CardImg.dao.find("select card_id,image_url from card_img where card_id = " + one.get("id"));
			List<String> imagesStr = Lists.newArrayList();
			for(CardImg img :images) {
				imagesStr.add(PicUtil.QINIU_DIR + img.getImageUrl());
				//img.setImageUrl((PicUtil.QINIU_DIR + img.getImageUrl()));
			}
			temp.put("images",  imagesStr);
			// @ 逻辑
//			if (StringUtil.empty(one.getStr("ids"))) {
//				temp.put("ids", Lists.newArrayList());
//			} else {
//				List<Users> users = Users.dao.find("select id,nick_name from users where id in (" + one.getStr("ids") +")");
//				temp.put("ids", users);
//			}
			//是否点赞
			if (user_id == 0L) {
				temp.put("is_like", 0);
			} else {
				CardLike like = CardLike.dao.findFirst("select * from card_like where card_id = " + one.getId() + " and status = 1 and " + " user_id =" + user_id);
				temp.put("is_like", like == null ? 0 :1 );
			}
			// 标签 topics
			if (StringUtil.empty(one.getStr("topic_id"))) {
				temp.put("topics", Lists.newArrayList());
			} else {
				List<Topics> topics = Topics.dao.find("select name from topics where id in (" + one.getStr("topic_id") +")");
				temp.put("topics", topics);
			}
			if (user_id == 0L) {
				temp.put("is_collect", 0);
			} else {
				CardCollect collect = CardCollect.dao.findFirst("select * from card_collect where card_id = " + one.getId() + " and status = 1 and " + " user_id =" + user_id);
				temp.put("is_collect", collect == null ? 0 :1 );
			}
//			temp.put("share_url", PicUtil.DOMAIN +"shares/index?card_id=" + one.getId());
//			temp.put("is_all", AlphaStr.is_all(StringUtil.decode(one.getContent()), 120, true));
			
//			temp.put("content_html", AlphaStr.sub_html(StringUtil.decode(one.getContent()), 120));
			
			String sql = "select u.*,ae.name as area_name, FROM_UNIXTIME(u.birthday,'%Y年%m月%d日') as birthday_string,u.is_true, CASE u.sex  WHEN '1' THEN '男' WHEN '2' THEN '女' WHEN '3' THEN 'CD' WHEN '4' THEN 'FtM' WHEN '5' THEN 'MtF'  ELSE '其他' END as sex_name ";
			sql += ", FROM_UNIXTIME(u.create_time,'%Y-%m-%d %H:%i') as create_time_string , FROM_UNIXTIME(u.vip_end_time,'%Y-%m-%d') as vip_end_time_string,u.auth_badge,u.talk_badge,u.vip_badpe,u.consume_badpe,u.credit from users as u left join area ae on ae.code = u.area_id where u.id = " + one.getUserId();
			
			Users user = Users.dao.findFirst(sql);
			if (user == null) {
				temp.put("user_data", Lists.newArrayList());
			} else {
				temp.put("user_data", Users.dao.info(user,"card_list"));
			}
			result.add(temp);
			
		}
		return result;
	}
	
	
	public ArrayList<Map<String, Object>> week_hotest(Long user_id, int page) {
		// TODO 私密空间 
		String andSql = " where a.is_show = 1 and a.create_time < "+ SystemClock.now()/1000L +" order by a.heat desc, a.create_time desc limit " + (page -1) * 10 +" , 10 " ;
		// a.discuss + 
		List<Card> cardList = dao.find(" select a.card_type,a.ids,a.id,a.user_id,a.content, FROM_UNIXTIME(a.create_time,'%m-%d') as create_time_string,a.href_val,a.images,a.views,a.discuss_cnt as discuss,a.heat,a.fabulous,a.tags_id,a.topic_id from card as a " + andSql);
		
		ArrayList<Map<String,Object>> result = Lists.newArrayList();
		for(Card one: cardList) {
			//Db.update("update card set t_views = t_views + 1 , views = views + 1 where id = " + one.get("id"));
			Map<String,Object> temp = Maps.newHashMap();
			temp.put("card_type",     one.get("card_type"));
			temp.put("id",            one.get("id"));
			temp.put("user_id",       one.get("user_id"));
			temp.put("content",       StringUtil.decode(one.getStr("content")) );
			temp.put("create_time",   one.get("create_time_string"));
			temp.put("href_val",      one.get("heat"));
			temp.put("views",         one.get("views"));
			temp.put("t_views",       one.get("t_views"));
			temp.put("discuss",       one.get("discuss"));
			temp.put("t_discuss",     one.get("t_discuss"));
			temp.put("heat",          one.get("heat"));
			temp.put("t_heat",        one.get("t_heat"));
			temp.put("fabulous",      one.get("fabulous"));
			temp.put("t_fabulous",    one.get("t_fabulous"));
			temp.put("tags_id",       one.get("tags_id"));
			temp.put("sort_heat",     one.get("sort_heat"));
			temp.put("tsort_heat",    one.get("tsort_heat"));
//			temp.put("img_check",     one.get("img_check"));
			temp.put("update_time",   one.get("update_time"));
			/*
			temp.put("alt_tj",        one.get("alt_tj"));
			temp.put("is_show",       one.get("is_show"));
			temp.put("is_top",        one.get("is_top"));
			temp.put("is_check",      one.get("is_check"));
			temp.put("is_private",    one.get("is_private"));
			*/
			//temp.put("images",        one.get("images"));  图片
			List<CardImg> images = CardImg.dao.find("select card_id,image_url from card_img where card_id = " + one.get("id"));
			List<String> imagesStr = Lists.newArrayList();
			for(CardImg img :images) {
				imagesStr.add(PicUtil.QINIU_DIR + img.getImageUrl());
				//img.setImageUrl((PicUtil.QINIU_DIR + img.getImageUrl()));
			}
			temp.put("images",  imagesStr);
			// @ 逻辑
//			if (StringUtil.empty(one.getStr("ids"))) {
//				temp.put("ids", Lists.newArrayList());
//			} else {
//				List<Users> users = Users.dao.find("select id,nick_name from users where id in (" + one.getStr("ids") +")");
//				temp.put("ids", users);
//			}
			//是否点赞
			if (user_id == 0L) {
				temp.put("is_like", 0);
			} else {
				CardLike like = CardLike.dao.findFirst("select * from card_like where card_id = " + one.getId() + " and status = 1 and " + " user_id =" + user_id);
				temp.put("is_like", like == null ? 0 :1 );
			}
			// 标签 topics
			if (StringUtil.empty(one.getStr("topic_id"))) {
				temp.put("topics", Lists.newArrayList());
			} else {
				List<Topics> topics = Topics.dao.find("select name from topics where id in (" + one.getStr("topic_id") +")");
				temp.put("topics", topics);
			}
			if (user_id == 0L) {
				temp.put("is_collect", 0);
			} else {
				CardCollect collect = CardCollect.dao.findFirst("select * from card_collect where card_id = " + one.getId() + " and status = 1 and " + " user_id =" + user_id);
				temp.put("is_collect", collect == null ? 0 :1 );
			}
			
			String sql = "select u.*,ae.name as area_name, FROM_UNIXTIME(u.birthday,'%Y年%m月%d日') as birthday_string,u.is_true, CASE u.sex  WHEN '1' THEN '男' WHEN '2' THEN '女' WHEN '3' THEN 'CD' WHEN '4' THEN 'FtM' WHEN '5' THEN 'MtF'  ELSE '其他' END as sex_name ";
			sql += ", FROM_UNIXTIME(u.create_time,'%Y-%m-%d %H:%i') as create_time_string , FROM_UNIXTIME(u.vip_end_time,'%Y-%m-%d') as vip_end_time_string,u.auth_badge,u.talk_badge,u.vip_badpe,u.consume_badpe,u.credit from users as u left join area ae on ae.code = u.area_id where u.id = " + one.getUserId();
			
			Users user = Users.dao.findFirst(sql);
			if (user == null) {
				temp.put("user_data", Lists.newArrayList());
			} else {
				temp.put("user_data", Users.dao.info(user, "card_list"));
			}
			result.add(temp);
		}
		return result;
	}

	public Map<String, Object> sel_one(String id, Long user_id) {
		Db.update("update card set t_views = t_views + 1 , views = views + 1 where id = " + id);
		
		String andSqlTime = "";
		if (user_id > 1L) {
			List<UserBlack> black_users = UserBlack.dao.find("select id,status,user_id,black_user_id from user_black where user_id = " + user_id);
			for(UserBlack one :black_users) {
				andSqlTime += " and a.user_id != "+ one.getBlackUserId();
			}
		}
		Card one = dao.findFirst(" select a.card_type,a.ids,a.id,a.user_id,a.content, FROM_UNIXTIME(a.create_time,'%m-%d') as create_time_string,a.href_val,a.images,a.views,a.discuss  as discuss,a.discuss_cnt,a.heat,a.fabulous,a.tags_id,a.topic_id from card as a where a.id = " + id + andSqlTime);
		Map<String,Object> temp = Maps.newHashMap();
		if (one != null) {
			temp.put("card_type",     one.get("card_type"));
			temp.put("id",            one.get("id"));
			temp.put("user_id",       one.get("user_id"));
			temp.put("content",       StringUtil.decode(one.getStr("content")));
			temp.put("create_time",   one.get("create_time_string"));
			temp.put("href_val",      one.get("href_val"));
			temp.put("views",         one.get("views"));
			temp.put("discuss",       one.get("discuss"));
			temp.put("heat",          one.get("heat"));
			temp.put("fabulous",      one.get("fabulous"));
			temp.put("tags_id",       one.get("tags_id"));
			//temp.put("images",        one.get("images"));  图片
			List<CardImg> images = CardImg.dao.find("select card_id,image_url from card_img where card_id = " + one.getId());
			List<String> imagesStr = Lists.newArrayList();
			for(CardImg img :images) {
				imagesStr.add(PicUtil.QINIU_DIR + img.getImageUrl());
				//img.setImageUrl((PicUtil.QINIU_DIR + img.getImageUrl()));
			}
			temp.put("images",  imagesStr);
			// @ 逻辑
			if (StringUtil.empty(one.getStr("ids"))) {
				temp.put("ids", Lists.newArrayList());
			} else {
				List<Users> users = Users.dao.find("select id,nick_name from users where id in (" + one.getStr("ids") +")");
				for(Users u : users) {
					u.setNickName(StringUtil.decode(u.getNickName()));
				}
				temp.put("ids", users);
			}
			//是否点赞
			if (user_id == 0L) {
				temp.put("is_like", 0);
			} else {
				CardLike like = CardLike.dao.findFirst("select * from card_like where card_id = " + one.getId() + " and status = 1 and " + " user_id =" + user_id);
				temp.put("is_like", like == null ? 0 :1 );
			}
			// 标签 topics
			if (StringUtil.empty(one.getStr("topic_id"))) {
				temp.put("topics", Lists.newArrayList());
			} else {
				List<Topics> topics = Topics.dao.find("select name from topics where id in (" + one.getStr("topic_id") +")");
				temp.put("topics", topics);
			}
			if (user_id == 0L) {
				temp.put("is_follow", 0);
			} else {
				UserFans follow = UserFans.dao.findFirst("select id from user_fans where user_id = "+ user_id +" and target_user_id = " + one.get("user_id")+ " order by id desc limit 1");
				temp.put("is_follow", follow == null ? 0: 1);
			}
			
			if (user_id == 0L) {
				temp.put("is_collect", 0);
			} else {
				CardCollect collect = CardCollect.dao.findFirst("select * from card_collect where card_id = " + one.getId() + " and status = 1 and " + " user_id =" + user_id);
				temp.put("is_collect", collect == null ? 0 :1 );
			}
			//temp.put("share_url", PicUtil.DOMAIN +"shares/index?card_id=" + one.getId());
			//temp.put("is_all", AlphaStr.is_all(StringUtil.decode(one.getContent()), 120, true));
			
			String sql = "select u.*,ae.name as area_name, FROM_UNIXTIME(u.birthday,'%Y年%m月%d日') as birthday_string,u.is_true, CASE u.sex  WHEN '1' THEN '男' WHEN '2' THEN '女' WHEN '3' THEN 'CD' WHEN '4' THEN 'FtM' WHEN '5' THEN 'MtF'  ELSE '其他' END as sex_name ";
			sql += ", FROM_UNIXTIME(u.create_time,'%Y-%m-%d %H:%i') as create_time_string , FROM_UNIXTIME(u.vip_end_time,'%Y-%m-%d') as vip_end_time_string,u.auth_badge,u.talk_badge,u.vip_badpe,u.consume_badpe,u.credit from users as u left join area ae on ae.code = u.area_id where u.id = " + one.getUserId();
			
			Users user = Users.dao.findFirst(sql);
			if (user == null) {
				temp.put("user_data", Lists.newArrayList());
			} else {
				temp.put("user_data", Users.dao.info(user, null));
			}

		}
		return temp;
	}

	public ArrayList<Map<String, Object>> sel_circle(Long user_id,int type,int page, int tags_id, int last_id) {
		
		 // 
		String orderSql = " order by a.create_time desc ";
		if (type == 1 ) {
			orderSql = " order by d.hot_val desc, a.create_time desc ";
		} else if (type == 2 ) {
			if (last_id > 0) {
				page = 1 ;
				Card card = Card.dao.findById(last_id);
				if (card != null) {
					orderSql = " and a.create_time < "+ card.getCreateTime() + "  order by a.create_time desc ";
				}
			}
		} else if (type == 3 ) {
			orderSql = " and c.sex = 2 order by a.create_time desc ";
		} 
		String andSql = " where a.tags_id =" + tags_id + " and  a.is_show = 1 and a.create_time < "+ SystemClock.now()/1000L + orderSql+ "  limit " + (page -1) * 10 +" , 10 " ;
		List<Card> cardList = dao.find(" select d.hot_val,a.card_type,a.ids,a.id,a.user_id,a.content, FROM_UNIXTIME(a.create_time,'%m-%d') as create_time_string,a.href_val,a.images,a.views,a.discuss,a.heat,a.fabulous,a.tags_id,a.topic_id from card as a left join users c on c.id = a.user_id left join (SELECT sum(hot_val) hot_val,card_id,create_time FROM card_hot GROUP BY card_id) d on d.card_id = a.id " + andSql);
		ArrayList<Map<String,Object>> result = Lists.newArrayList();
		for(Card one: cardList) {
			Db.update("update card set t_views = t_views + 1 , views = views + 1 where id = " + one.get("id"));
			Map<String,Object> temp = Maps.newHashMap();
			temp.put("card_type",     one.get("card_type"));
			temp.put("id",            one.get("id"));
			temp.put("user_id",       one.get("user_id"));
			temp.put("content",       one.get("content"));
			temp.put("create_time",   one.get("create_time_string"));
			temp.put("href_val",      one.get("href_val"));
			temp.put("views",         one.get("views"));
			temp.put("discuss",       one.get("discuss"));
			temp.put("heat",          one.get("heat"));
			temp.put("fabulous",      one.get("fabulous"));
			temp.put("tags_id",       one.get("tags_id"));
			temp.put("hot_val",       one.get("hot_val"));
			//temp.put("images",        one.get("images"));  图片
			List<CardImg> images = CardImg.dao.find("select card_id,image_url from card_img where card_id = " + one.getId());
			for(CardImg img :images) {
				img.setImageUrl((PicUtil.QINIU_DIR + img.getImageUrl()));
			}
			temp.put("images",  images);
			// @ 逻辑
			if (StringUtil.empty(one.getStr("ids"))) {
				temp.put("ids", Lists.newArrayList());
			} else {
				List<Users> users = Users.dao.find("select id,nick_name from users where id in (" + one.getStr("ids") +")");
				for(Users u : users) {
					u.setNickName(StringUtil.decode(u.getNickName()));
				}
				temp.put("ids", users);
			}
			//是否点赞
			if (user_id == 0L) {
				temp.put("is_like", 0);
			} else {
				CardLike like = CardLike.dao.findFirst("select * from card_like where card_id = " + one.getId() + " and status = 1 and " + " user_id =" + user_id);
				temp.put("is_like", like == null ? 0 :1 );
			}
			// 标签 topics
			if (StringUtil.empty(one.getStr("topic_id"))) {
				temp.put("topics", Lists.newArrayList());
			} else {
				List<Topics> topics = Topics.dao.find("select name from topics where id in (" + one.getStr("topic_id") +")");
				temp.put("topics", topics);
			}
			if (user_id == 0L) {
				temp.put("is_collect", 0);
			} else {
				CardCollect collect = CardCollect.dao.findFirst("select * from card_collect where card_id = " + one.getId() + " and status = 1 and " + " user_id =" + user_id);
				temp.put("is_collect", collect == null ? 0 :1 );
			}
			temp.put("share_url", PicUtil.DOMAIN +"shares/index?card_id=" + one.getId());
			temp.put("is_all", AlphaStr.is_all(StringUtil.decode(one.getContent()), 120, true));
			
			String sql = "select u.*,ae.name as area_name, FROM_UNIXTIME(u.birthday,'%Y年%m月%d日') as birthday_string,u.is_true, CASE u.sex  WHEN '1' THEN '男' WHEN '2' THEN '女' WHEN '3' THEN 'CD' WHEN '4' THEN 'FtM' WHEN '5' THEN 'MtF'  ELSE '其他' END as sex_name ";
			sql += ", FROM_UNIXTIME(u.create_time,'%Y-%m-%d %H:%i') as create_time_string , FROM_UNIXTIME(u.vip_end_time,'%Y-%m-%d') as vip_end_time_string,u.auth_badge,u.talk_badge,u.vip_badpe,u.consume_badpe,u.credit from users as u left join area ae on ae.code = u.area_id where u.id = " + one.getUserId();
			
			Users user = Users.dao.findFirst(sql);
			if (user == null) {
				temp.put("user_data", Lists.newArrayList());
			} else {
				temp.put("user_data", Users.dao.info(user,null));
			}
			result.add(temp);
			
		}
		return result;
	}


	public ArrayList<Map<String, Object>> follow(int page ,Long user_id) {
		List<Card> cardList = null;
		if (user_id > 0) {
			String andSql = " where a.is_show = 1 and a.create_time < "+ SystemClock.now()/1000L ;
			List<UserFans> fans = UserFans.dao.find("select target_user_id from user_fans where user_id = " + user_id);
			if (fans == null || fans.size() == 0) {
				return Lists.newArrayList();
			} else {
				andSql += " and (";
				for(int i = 0 ; i < fans.size() ; i++ ) {
					if (i == 0) {
						andSql += " a.user_id = "+ fans.get(i).getTargetUserId();	
					} else {
						andSql += " or a.user_id = "+ fans.get(i).getTargetUserId();
					}
					
				}
				andSql += ")";
			}
			andSql = andSql	+ " order by  a.create_time desc limit " + (page -1) * 10 +" , 10 " ;
			cardList = dao.find("select a.card_type,a.ids,a.id,a.user_id,a.content, FROM_UNIXTIME(a.create_time,'%m-%d') as create_time_string,a.href_val,a.images,a.views,a.discuss  as discuss,a.heat,a.fabulous,a.tags_id,a.topic_id from card as a " + andSql);
		}
		if (cardList == null || cardList.size() == 0) {
			cardList = dao.find(" select d.hot_val,a.card_type,a.ids,a.id,a.user_id,a.content, FROM_UNIXTIME(a.create_time,'%m-%d') as create_time_string,a.href_val,a.images,a.views,a.discuss as discuss,a.heat,a.fabulous,a.tags_id,a.topic_id from card as a left join (SELECT sum(hot_val) hot_val,card_id,create_time FROM card_hot GROUP BY card_id) d on d.card_id = a.id where a.user_id = 200 order by d.hot_val desc, a.create_time desc limit " + (page -1) * 10 +" , 10 " );
		}
		ArrayList<Map<String,Object>> result = Lists.newArrayList();
		for(Card one: cardList) {
			Db.update("update card set t_views = t_views + 1 , views = views + 1 where id = " + one.get("id"));
			Map<String,Object> temp = Maps.newHashMap();
			temp.put("card_type",     one.get("card_type"));
			temp.put("id",            one.get("id"));
			temp.put("user_id",       one.get("user_id"));
			temp.put("content",       StringUtil.decode(one.getStr("content")));
			temp.put("create_time",   one.get("create_time_string"));
			temp.put("href_val",      one.get("href_val"));
			temp.put("views",         one.get("views"));
			temp.put("discuss",       one.get("discuss"));
			temp.put("heat",          one.get("heat"));
			temp.put("fabulous",      one.get("fabulous"));
			temp.put("tags_id",       one.get("tags_id"));
			//temp.put("images",        one.get("images"));  图片
			List<CardImg> images = CardImg.dao.find("select card_id,image_url from card_img where card_id = " + one.getId());
			List<String> imagesStr = Lists.newArrayList();
			for(CardImg img :images) {
				imagesStr.add(PicUtil.QINIU_DIR + img.getImageUrl());
				//img.setImageUrl((PicUtil.QINIU_DIR + img.getImageUrl()));
			}
			temp.put("images",  imagesStr);
			// @ 逻辑
			if (StringUtil.empty(one.getStr("ids"))) {
				temp.put("ids", Lists.newArrayList());
			} else {
				List<Users> users = Users.dao.find("select id,nick_name from users where id in (" + one.getStr("ids") +")");
				for(Users u : users) {
					u.setNickName(StringUtil.decode(u.getNickName()));
				}
				temp.put("ids", users);
			}
			//是否点赞
			if (user_id == 0L) {
				temp.put("is_like", 0);
			} else {
				CardLike like = CardLike.dao.findFirst("select * from card_like where card_id = " + one.getId() + " and status = 1 and " + " user_id =" + user_id);
				temp.put("is_like", like == null ? 0 :1 );
			}
			// 标签 topics
			if (StringUtil.empty(one.getStr("topic_id"))) {
				temp.put("topics", Lists.newArrayList());
			} else {
				List<Topics> topics = Topics.dao.find("select name from topics where id in (" + one.getStr("topic_id") +")");
				temp.put("topics", topics);
			}
			if (user_id == 0L) {
				temp.put("is_collect", 0);
			} else {
				CardCollect collect = CardCollect.dao.findFirst("select * from card_collect where card_id = " + one.getId() + " and status = 1 and " + " user_id =" + user_id);
				temp.put("is_collect", collect == null ? 0 :1 );
			}
			//temp.put("share_url", PicUtil.DOMAIN +"shares/index?card_id=" + one.getId());
//			temp.put("is_all", AlphaStr.is_all(StringUtil.decode(one.getContent()), 120, true));
			
			String sql = "select u.*,ae.name as area_name, FROM_UNIXTIME(u.birthday,'%Y年%m月%d日') as birthday_string,u.is_true, CASE u.sex  WHEN '1' THEN '男' WHEN '2' THEN '女' WHEN '3' THEN 'CD' WHEN '4' THEN 'FtM' WHEN '5' THEN 'MtF'  ELSE '其他' END as sex_name ";
			sql += ", FROM_UNIXTIME(u.create_time,'%Y-%m-%d %H:%i') as create_time_string , FROM_UNIXTIME(u.vip_end_time,'%Y-%m-%d') as vip_end_time_string,u.auth_badge,u.talk_badge,u.vip_badpe,u.consume_badpe,u.credit from users as u left join area ae on ae.code = u.area_id where u.id = " + one.getUserId();
			
			Users user = Users.dao.findFirst(sql);
			if (user == null) {
				temp.put("user_data", Lists.newArrayList());
			} else {
				temp.put("user_data", Users.dao.info(user,null));
			}
			result.add(temp);
			
		}
		return result;
	}
	
}